package com.barneyx.mshinfoplatform.event.eventListener;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.barneyx.mshinfoplatform.entity.info.InfoEntity;
import com.barneyx.mshinfoplatform.event.MshEvent;
import com.barneyx.mshinfoplatform.util.AreaUtil;
import com.barneyx.mshinfoplatform.util.InfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;

@Component
@Slf4j
public class InfoEventListener {
    @Resource
    private AreaUtil areaUtil;
    @Resource
    private InfoUtil.Setting settingUtil;

    @EventListener(classes = MshEvent.class, condition = "#event.eventType.value.equalsIgnoreCase('EVENT_INFO_CREATE_AUDIT')")
    public void onInfoCreateEvent(MshEvent<InfoEntity> event) {
        log.error("接收到了来自用户的信息审核申请....");
        log.error(event.toString());
        String email_str = settingUtil.getAreaManageUserEmailByInfoId(event.getData().getAreaId());
        if(email_str!=null){
            String sendId = MailUtil.send(email_str, StrUtil.format("用户{}刚刚发了一条{}信息,请审核员审核.",
                    event.getData().getInfoPostName(),
                    event.getData().getCateName()
            ), event.getData().getRemark(), true);
        }

    }

    @EventListener(classes = MshEvent.class, condition = "#event.eventType.value.equalsIgnoreCase('EVENT_INFO_UPDATE_AUDIT')")
    public void onInfoUpdateEvent(MshEvent<InfoEntity> event) {
        log.error("接收到了来自用户的信息审核申请....");
        log.error(event.toString());
        String email_str = settingUtil.getAreaManageUserEmailByInfoId(event.getData().getAreaId());
        if(email_str!=null){
            String sendId = MailUtil.send(email_str, StrUtil.format("用户{}刚刚发了一条{}信息,请审核员审核.",
                    event.getData().getInfoPostName(),
                    event.getData().getCateName()
            ), event.getData().getRemark(), true);
            log.error(sendId);
        }
    }
}
